package com.company.project.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.ExhibitionGoodsEntity;
import com.company.project.service.ExhibitionGoodsService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
 * 商品购物
 *
 * @author HKX
 * @email *****@mail.com
 * @date 2022-05-03 09:50:51
 */
@Controller
@RequestMapping("/")
public class ExhibitionGoodsController {
    @Autowired
    private ExhibitionGoodsService exhibitionGoodsService;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/exhibitionGoods")
    public String exhibitionGoods() {
        return "exhibitiongoods/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("exhibitionGoods/add")
    @RequiresPermissions("exhibitionGoods:add")
    @ResponseBody
    public DataResult add(@RequestBody ExhibitionGoodsEntity exhibitionGoods){
        exhibitionGoodsService.save(exhibitionGoods);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("exhibitionGoods/delete")
    @RequiresPermissions("exhibitionGoods:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        exhibitionGoodsService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("exhibitionGoods/update")
    @RequiresPermissions("exhibitionGoods:update")
    @ResponseBody
    public DataResult update(@RequestBody ExhibitionGoodsEntity exhibitionGoods){
        exhibitionGoodsService.updateById(exhibitionGoods);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("exhibitionGoods/listByPage")
    @RequiresPermissions("exhibitionGoods:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody ExhibitionGoodsEntity exhibitionGoods){
        Page page = new Page(exhibitionGoods.getPage(), exhibitionGoods.getLimit());
        LambdaQueryWrapper<ExhibitionGoodsEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(ExhibitionGoodsEntity::getId, exhibitionGoods.getId());
        if (StrUtil.isNotBlank(exhibitionGoods.getGoodsName())){
            queryWrapper.like(ExhibitionGoodsEntity::getGoodsName,exhibitionGoods.getGoodsName());
        }
        queryWrapper.orderByDesc(ExhibitionGoodsEntity::getGoodsCount);
        queryWrapper.eq(ExhibitionGoodsEntity::getDeleted,1);
        queryWrapper.eq(ExhibitionGoodsEntity::getGoodsStatus,1);

        IPage<ExhibitionGoodsEntity> iPage = exhibitionGoodsService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
